package com.flexcloudsdk.backends;

import java.util.logging.Logger;
import com.google.appengine.api.backends.BackendService;
import com.google.appengine.api.backends.BackendServiceFactory;

public class CloudBackendService {
	private String sourceClass;
	private String sourceMethod;
	private static final Logger log = Logger.getLogger(CloudBackendService.class.getName());

	private BackendService service;
	public BackendService getService() {
		return service;
	}
	public void setService(BackendService service) {
		this.service = service;
	}
	
	public CloudBackendService() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudBackendService.class.getName();
		log.info("CloudBackendService has been created by BlazeDS");
		service = BackendServiceFactory.getBackendService();
	}
	
	public String getCurrentBackend() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return service.getCurrentBackend();
	}
	
    public String getBackendAddress(String backend) {
     	sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
     	return service.getBackendAddress(backend);
    }
    
    public String getBackendAddress(String backend, int  instance) {
     	sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
 		log.info("Executing: " + sourceClass + "." + sourceMethod);
     	return service.getBackendAddress(backend, instance);
    }
    
    public int getCurrentInstance() {
     	sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
     	return service.getCurrentInstance();
    }
  
    @Override
	public String toString() {
     	sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
     	return service.toString();
    }
}
